class Particle{
  constructor(love,conf){
    this.love = love
    this.x = 0
    this.y = 0
    this.dx =  0
    this.dy = 0
    this.rx =  1
    this.ry = 1
    this.ax = 0
    this.ay =  0
    this.size =  10
    this.ds = 0
    this.life = 1000
    let info = wx.getSystemInfoSync()
    this.color = {r:255,g:255,b:255,a:1}
    this.type = "point"
    this.da = 0
    this.lineWidth = 1
    this.setData(conf)
  }
  setData(conf){
    for (var k in conf){
      var v = conf[k]
      if (typeof(v) == "string"){
        this[k]  = v
      } else if (typeof(v) == "number"){
        this[k] = v
      } else if (typeof (v.r) != "undefined") {
        this[k] = {r :v.r, g:v.g,b:v.b,a:v.a || 1}
      } else if (typeof (v) == "object") {
        this[k] = this.love.math.random(v[0], v[1])
      } else {
        this[k] = v
      }
      //console.log(k, v,this[k])
    }
  }
  update(dt){
    if (this.destroyed) {return}
    this.life -= dt
    if (this.life<0){this.destroyed = true}
    this.dx = (this.dx + this.ax) * this.rx
    this.x += this.dx*dt
    this.dy = (this.dy + this.ay) * this.ry
    this.y += this.dy*dt
    this.size += this.ds
    this.size = this.size<0? 0 : this.size
    this.color.a = this.color.a + this.da
    if (this.color.a<0) {
      this.destroyed = true
    }
  }
  draw(){
    let love = this.love
    love.graphics.setColor(this.color)
    love.graphics.setLineWidth(this.lineWidth)
    //love.graphics.setColor("white")
    //love.graphics.print(this.color,300,200)
    switch(this.type){
      case "point": 
        love.graphics.rectangle("fill", this.x - this.size / 2, this.y - this.size / 2, this.size, this.size)
        break
      case "rect" :
        love.graphics.rectangle("line", this.x - this.size / 2, this.y - this.size / 2, this.size, this.size)
        break
      case "circle":
        love.graphics.circle("fill", this.x , this.y , this.size)
        break
    }
    
  }
}

var p = 0
export default class PS {
  constructor(_love){
    this.love = _love
    this.buff = []
  }
  newParticle(conf,count = 1){
    if (count<=0){
      return
    }else if(count<1){
      p = p + count
      if (p>=1) {
        p = 0
        this.buff.push(new Particle(this.love,conf))
      }
    }else {
      for (var i = 0;i<count;i++){
        this.buff.push(new Particle(this.love,conf))
      }
    }
  }
  update(dt){
    var newP = []
    for (var i in this.buff){
      let p = this.buff[i]
      p.update(dt)
      if (!p.destroyed){
        newP.push(p)
      }
    }
    this.buff = newP
  }
  draw(){
    for (var i in this.buff) {
      let p = this.buff[i]
      p.draw()
    }
  }
}